package spring.chatroom.java_chatroom.model;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import spring.chatroom.java_chatroom.model.util.AiResQueue;
import spring.chatroom.java_chatroom.pojo.model.MsgInfo;

@Slf4j
@Service
public class ChatAi {

    @Autowired
    ChatClient chatClient;

    @Value("${hadage.ai.id}")
    private Integer id;
    @Value("${hadage.ai.name}")
    String name;


    public void chat(MsgInfo msg) {

        //发送给ai 获取到 他的回复
        String res = chatClient.prompt(msg.getContent())
                .advisors(spc -> spc.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, msg.getSessionId()))
                .call().content();

        //构造ai请求的放回
        MsgInfo m = new MsgInfo(msg);
        m.setUserId(id);
        m.setUserName(name);
        m.setContent(res);

        //将消息添加到队列中
        AiResQueue.put(m);
    }
}
